package com.core.bean;

import com.core.dao.AbstractDaoFactory;
import com.core.dao.IOnlineDao;

import java.util.Date;
public abstract class Online {

    private String id;

    private User user;
    private String jsonUser;

    private Date loginTime;

    private String ip;

    private int port;

    private String devInfo;

    private String os;

    private String toKen;

    public Online() {
    }


    @Override
    public String toString() {
        return "Online{" +
                "id='" + id + '\'' +
                ", user=" + user +
                ", jsonUser='" + jsonUser + '\'' +
                ", loginTime=" + loginTime +
                ", ip='" + ip + '\'' +
                ", port=" + port +
                ", devInfo='" + devInfo + '\'' +
                ", os='" + os + '\'' +
                ", toKen='" + toKen + '\'' +
                '}';
    }

    public String getJsonUser() {
        return jsonUser;
    }

    public void setJsonUser(String jsonUser) {
        this.jsonUser = jsonUser;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getDevInfo() {
        return devInfo;
    }

    public void setDevInfo(String devInfo) {
        this.devInfo = devInfo;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public String getToKen() {
        return toKen;
    }

    public void setToKen(String toKen) {
        this.toKen = toKen;
    }

    public abstract void login()throws Exception;

    /**
     * 退出登录。
     * 只会退出当前设备的会话，如果同时登录多个设备其他设备会话不受影响。
     * 退出时是通过token作为条件定位会话。
     * @throws Exception
     */
    public  void loginOut()throws Exception {
        IOnlineDao dao = getDao();
        Online onlineUser = dao.getByUserName(this.getUser().getName());
        if (onlineUser.getToKen().equals(this.getToKen()))
            dao.remove(onlineUser.getId());
    }

    protected IOnlineDao getDao(){
        return AbstractDaoFactory.create(AbstractDaoFactory.MYBATIS_MYSQL_DAO).getOnlineDao();
    }


}
